package com.piketao.controller;

import com.piketao.common.pojo.PageInfo;
import com.piketao.dao.Goods;
import com.piketao.search.SearchService;
import com.piketao.vo.SearchRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author: xinglongge
 * @Date: 2020/3/10 11 54
 * @Description:
 */
@Controller
public class SearchController {
    private final SearchService searchService;

    @Autowired
    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    @PostMapping("page")
    public ResponseEntity<PageInfo<Goods>> search(@RequestBody SearchRequest search){
        PageInfo<Goods> result  = searchService.search(search);
        if (result==null|| CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);

    }
}
